﻿using System;
using System.Collections.Generic;
using CodeFirstMVC3.Core;
using CodeFirstMVC3.DataAccess.Model;
using CodeFirstMVC3.Domain.DataAccess;
using CodeFirstMVC3.Domain.Model;
using CodeFirstMVC3.Domain.Service;

namespace CodeFirstMVC3.Service
{
    public class RoleService : IRoleService
    {
        private readonly IRepository<Role> _repository;

        public RoleService(IRepository<Role> repository)
        {
            _repository = repository;
        }

        public IRole New()
        {
            return _repository.NewEntity();
        }

        public void Add(IRole entity)
        {
            _repository.Add(entity as Role);
            _repository.Save();
        }

        public void Edit(IRole entity)
        {
            Role m = entity as Role;
            _repository.Save();
        }

        public void Remove(IRole entity)
        {
            _repository.Remove(entity as Role);
            _repository.Save();
        }

        public IRole Get(int id)
        {
            return _repository.Get(p => p.Id == id);
        }

        public IRole Get(Func<IRole, bool> predicate)
        {
            return _repository.Get(predicate);
        }

        public int GetCount(Func<IRole, bool> perdicate = null)
        {
            if (perdicate == null)
                return _repository.GetCount(p => true);
            return _repository.GetCount(perdicate);
        }

        public IEnumerable<IRole> GetAll(Func<IRole, bool> predicate = null, int start = 0, int count = ApplicationSettings.PageSize)
        {
            if (predicate == null)
                return _repository.GetAll(p => true);

            if (start == 0)
            {
                return _repository.GetAll(predicate);
            }

            return _repository.GetAll(predicate, start, count);
        }
    }
}
